package com.evi.da.question.ask;

import com.evi.da.data.generated.Response;
import com.evi.da.data.unmarshaller.XmlResponseReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;

@Component
public class QuestionSubmitterImpl implements QuestionSubmitter
{
	private static final Logger logger = LoggerFactory.getLogger(QuestionSubmitterImpl.class);

	private ServerConnector serverConnector;

	@Autowired
	public QuestionSubmitterImpl(ServerConnector serverConnector)
	{
		this.serverConnector = serverConnector;
	}

	@Override
	public Response sendQuestion(String question)
	{
		Response response = null;
		try
		{
			InputStream in = serverConnector.getServerConnector(question);

			if (in != null)
			{
				final XmlResponseReader xmlResponseReader = new XmlResponseReader();
				response = xmlResponseReader.read(in);
				in.close();
			}

		} catch (IOException e)
		{
			logger.error("Problem unmarshalling inputstream: ", e);
		}

		return response;
	}

}
